using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Marlimer.Common;
using Marlimer.Common.Entidades;

namespace Marlimer.Data.Comercial
{
    public class com_Modulo
    {
        public DataTable getDTModulo()
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsdtModulo";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            da.SelectCommand = sqlcmd;
            da.Fill(ds);
            return ds.Tables[0];
        }

        public resulOMsgError getModuloByCod(String ccod_modulo)
        {
            resulOMsgError resul = new resulOMsgError(false,"",null); 
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            sqlcmd.CommandText = "spsModuloByCod";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.Parameters.Add("ccod_modulo", SqlDbType.NVarChar, 15);
            sqlcmd.Parameters["ccod_modulo"].Value = ccod_modulo;

            try
            {
                da.SelectCommand = sqlcmd;
                da.Fill(ds);
                if (ds.Tables[0] != null)
                {
                    resul.clase = ds.Tables[0];
                }
                resul.resul = true; 
            }
            catch (Exception ex)
            {
                resul.message = ex.Message; 
            }
            return resul;
        }

        public DataTable consultaModulo(String ccod_modulo, int ccod_ruta, string cnom_modulo, int ndia_visita, int flg_anulados)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsConsultaModulo";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_modulo", SqlDbType.NVarChar, 15);
            sqlcmd.Parameters.Add("@ccod_ruta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnom_modulo", SqlDbType.NVarChar, 25);
            sqlcmd.Parameters.Add("@ndia_visita", SqlDbType.Int);
            sqlcmd.Parameters.Add("@flg_anulados", SqlDbType.Int);

            sqlcmd.Parameters["@ccod_modulo"].Value = ccod_modulo;
            sqlcmd.Parameters["@ccod_ruta"].Value = ccod_ruta;
            sqlcmd.Parameters["@cnom_modulo"].Value = cnom_modulo;
            sqlcmd.Parameters["@ndia_visita"].Value = ndia_visita;
            sqlcmd.Parameters["@flg_anulados"].Value = flg_anulados;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public resulMsgError guardar(InfoModulo modulo)
        {
            resulMsgError resul = new resulMsgError(false, "");

            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandText = "spiaModulo";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_modulo", SqlDbType.NVarChar, 15);
            sqlcmd.Parameters.Add("@cnom_modulo", SqlDbType.NVarChar, 30);
            sqlcmd.Parameters.Add("@ccod_ruta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ndia_visita", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cdia_visita", SqlDbType.NVarChar, 15);
            sqlcmd.Parameters.Add("@cstatus", SqlDbType.NChar, 1);
            sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
            sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cmodo", SqlDbType.Int);

            sqlcmd.Parameters["@ccod_modulo"].Value = modulo.ccod_modulo;
            sqlcmd.Parameters["@cnom_modulo"].Value = modulo.cnom_modulo;
            sqlcmd.Parameters["@ccod_ruta"].Value = modulo.ccod_ruta;
            sqlcmd.Parameters["@ndia_visita"].Value = modulo.ndia_visita;
            sqlcmd.Parameters["@cdia_visita"].Value = modulo.cdia_visita;
            sqlcmd.Parameters["@cstatus"].Value = modulo.cstatus;
            sqlcmd.Parameters["@cpc_crea"].Value = Session.getComputerName();
            sqlcmd.Parameters["@cuser_crea"].Value = Session.getInfousuario().ccod_user;
            sqlcmd.Parameters["@cmodo"].Value = modulo.cmodo;

            try
            {
                sqlcmd.ExecuteNonQuery();
                //verificamos el tipo de mensaje a mostrar
                if (modulo.cmodo == 3)
                {
                    resul.message = "Anulado satisfactoriamente";
                }
                else
                {
                    resul.message = "Se guardo satisfactoriamente";
                }
                resul.resul = true;
            }
            catch (Exception ex)
            {
                resul.message = ex.Message;
            }
            return resul;
        }
    }
}
